﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight.Messaging;
using Sample.ViewModel;
using WorkingFileManager.MVVM;

namespace Sample.View
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Closing += MainWindow_Closing;

            this.DataContext = new MainWindowViewModel();

            ////////////////////////////////////////////////////////////////////////////////////////////////
            // SUBSCRIBE TO MESSAGES FROM VIEWMODEL
            ////////////////////////////////////////////////////////////////////////////////////////////////
            Messenger.Default.Register<NewFileMessage>(
                this,
                msg =>
                {
                    WorkingFileManager.ViewMethods.HandleNewFileMessage(msg.nfmparams, this);
                }
            );

            Messenger.Default.Register<OpenFileMessage>(
                this,
                msg =>
                {
                    WorkingFileManager.ViewMethods.HandleOpenFileMessage(msg.ofdparams, this);
                }
            );

            Messenger.Default.Register<OpenRecentFileMessage>(
                this,
                msg =>
                {
                    WorkingFileManager.ViewMethods.HandleOpenRecentFileMessage(msg.orfparams, this);
                }
            );

            Messenger.Default.Register<SaveAsFileMessage>(
                this,
                msg =>
                {
                    WorkingFileManager.ViewMethods.HandleSaveFileMessage(msg.sfdParams, this);
                }
            );

            Messenger.Default.Register<ExitMessage>(
                this,
                msg =>
                {
                    WorkingFileManager.ViewMethods.HandleExitMessage(msg.exitParams, this);
                }
            );

        }

        void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            var viewModel = this.DataContext as MainWindowViewModel;
            viewModel.DefaultFileCommandImplementations.CmdExit.Execute(null);
            e.Cancel = true;
        }
    }
}
